package com.ktjy.ktmall.dao.impl;

import com.ktjy.ktmall.dao.BaseDao;
import com.ktjy.ktmall.dao.WxUserDao;
import com.ktjy.ktmall.entity.WxUser;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @version v1.0
 * @Author 郴州科泰
 * @Create 2024-07-27 - 14:31
 */
public class WxUserDaoImpl extends BaseDao implements WxUserDao{

    /**
     * 根据手机号查询微信用户详情信息
     * @param mobile 手机号
     * @return 返回查询到的微信用户信息
     */
    @Override
    public WxUser findByMobile(String mobile) {
        WxUser user = null;
        String sql = "select * from `t_wx_user` where `mobile` = ?";
        rs = super.executeQuery(sql, mobile);
        try {
            if (rs.next()) {
                user = new WxUser(rs.getInt("id"), rs.getString("username"), rs.getInt("gender"), rs.getString("birthday"), rs.getString("nickname"),rs.getString("mobile"),rs.getString("avatar"), rs.getString("fullLocation"), rs.getString("profession"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
        }
        return user;
    }

    // 新增微信用户信息
    @Override
    public int insert(WxUser user) {
        String sql = "insert into `t_wx_user`(username, password, `last_login_time`, `last_login_ip`, nickname, mobile, avatar, open_id) values(?,?,?,?,?,?,?,?)";
        return super.executeUpdate(sql, user.getUsername(),user.getPassword(), user.getLastLoginTime(), user.getLastLoginIp(), user.getNickname(),user.getMobile(),user.getAvatar(), user.getOpenId());
    }

    /**
     * 修改微信用户的最后一次登录时间
     * @param id 为微信用户id
     */
    @Override
    public int updateLastLoginTime(int id) {
        String sql = "update `t_wx_user` set `last_login_time` = ? where id = ?";
        // 得到yyyy-MM-dd格式的当前时间
        String lastLoginTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        return super.executeUpdate(sql, lastLoginTime , id);
    }

    /**
     * 修改微信会员信息
     * @param wxUser 微信会员信息
     */
    @Override
    public int updateWxUser(WxUser wxUser) {
        String sql = "update `t_wx_user` set username=?,nickname=?,avatar=?,gender=?,birthday=?,fullLocation=?,profession=? where id = ?";
        return super.executeUpdate(sql, wxUser.getUsername(),wxUser.getNickname(),wxUser.getAvatar(),wxUser.getGender(),wxUser.getBirthday(),wxUser.getFullLocation(),wxUser.getProfession(),wxUser.getId());
    }

    // 通过微信用户id查询微信用户详情
    @Override
    public WxUser findById(int userId) {
        WxUser user = null;
        String sql = "select * from `t_wx_user` where `id` = ?";
        rs = super.executeQuery(sql, userId);
        try {
            if (rs.next()) {
                user = new WxUser(rs.getInt("id"), rs.getString("username"), rs.getInt("gender"), rs.getString("birthday"), rs.getString("nickname"),rs.getString("mobile"),rs.getString("avatar"), rs.getString("fullLocation"), rs.getString("profession"));
                user.setOpenId(rs.getString("open_id"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return user;
    }
}
